//
//  MCRechargeViewController.m
//  TLYL
//
//  Created by MC on 2017/6/13.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCRechargeViewController.h"
#import "MCMineCellModel.h"
#import "MCRechargeHeaderView.h"
#import "MCRechargeFooterView.h"
#import "MCRechargePaymentTypeTableViewCell.h"
#import "MCRechargePayMoneyTableViewCell.h"
#import "MCRechargeTableView.h"

@interface MCRechargeViewController ()
<
UITextFieldDelegate,
UITableViewDelegate,
UITableViewDataSource,
MCRechargeFooterDelegate,
MCRechargePaymentTypeDelegate
>

@property(nonatomic, strong)MCRechargeTableView *tableView;
@property(nonatomic, strong)NSMutableArray*marr_Section;
@property(nonatomic, strong)MCRechargeHeaderView * view_rechargeHeader;
@property(nonatomic, strong)MCRechargeFooterView * view_rechargeFooter;
@property(nonatomic, strong)UITextField  * textField;
@end

@implementation MCRechargeViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setProperty];
    
    [self createUI];
    
    [self loadData];
    
    [self addNoticeForKeyboard];

}
#pragma mark - 键盘通知
- (void)addNoticeForKeyboard {
    
    //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark==================setProperty======================
-(void)setProperty{
    self.view.userInteractionEnabled = YES;
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"充值";
    _marr_Section= [[NSMutableArray alloc]init];
    
}

#pragma mark==================createUI======================
-(void)createUI{
    _tableView = [[MCRechargeTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.and.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
    }];
    
}

#pragma mark==================loadData======================
-(void)loadData{
    NSMutableArray * marr_Model=[[NSMutableArray alloc]init];

    CellModel* model0 =[[CellModel alloc]init];
    model0.reuseIdentifier = NSStringFromClass([MCRechargePaymentTypeTableViewCell class]);
    model0.className=NSStringFromClass([MCRechargePaymentTypeTableViewCell class]);
    model0.height = [MCRechargePaymentTypeTableViewCell computeHeight:nil];
    model0.selectionStyle=UITableViewCellSelectionStyleNone;
    model0.accessoryType=UITableViewCellAccessoryNone;
    /*
     * 传递参数
     */
    model0.userInfo = @[@"微信支付",@"支付宝支付",@"在线支付",@"网银支付1",@"网银支付2"];
    [marr_Model addObject:model0];

    CellModel* model1 =[[CellModel alloc]init];
    model1.reuseIdentifier = NSStringFromClass([MCRechargePayMoneyTableViewCell class]);
    model1.className=NSStringFromClass([MCRechargePayMoneyTableViewCell class]);
    model1.height = [MCRechargePayMoneyTableViewCell computeHeight:nil];
    model1.selectionStyle=UITableViewCellSelectionStyleNone;
    model1.accessoryType=UITableViewCellAccessoryNone;
    /*
     * 传递参数
     */
    //    model1.userInfo = self.marr_title[i];
    [marr_Model addObject:model1];
    
    SectionModel *model_Section=[SectionModel sectionModelWithTitle:@"" cells:marr_Model];
    model_Section.headerhHeight=[MCRechargeHeaderView computeHeight:nil];
    model_Section.footerHeight=[MCRechargeFooterView computeHeight:nil];
    [_marr_Section addObject:model_Section];
    
    [_tableView reloadData];
    
    self.view_rechargeHeader.dataSource=nil;
    
    
}
#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _marr_Section.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.cells.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.footerHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return self.view_rechargeHeader;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return self.view_rechargeFooter;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    return cm.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
    if ([cm.className isEqualToString:NSStringFromClass([MCRechargePaymentTypeTableViewCell class])]) {
        
        MCRechargePaymentTypeTableViewCell *ex_cell=(MCRechargePaymentTypeTableViewCell *)cell;
        ex_cell.delegate=self;
        
        if (cm.userInfo) {

            ex_cell.dataSource=cm.userInfo;
        }

    }else if([cm.className isEqualToString:NSStringFromClass([MCRechargePayMoneyTableViewCell class])]){
        MCRechargePayMoneyTableViewCell *ex_cell=(MCRechargePayMoneyTableViewCell *)cell;
        
        _textField=ex_cell.textField;
        _textField.delegate=self;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark-Header
-(MCRechargeHeaderView *)view_rechargeHeader{
    if (!_view_rechargeHeader) {
        _view_rechargeHeader =[[MCRechargeHeaderView alloc]init];
    }
    return _view_rechargeHeader;
}

#pragma mark-Footer
-(MCRechargeFooterView *)view_rechargeFooter{
    if (!_view_rechargeFooter) {
        _view_rechargeFooter =[[MCRechargeFooterView alloc]init];
        _view_rechargeFooter.delegate=self;
    }
    return _view_rechargeFooter;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [_textField resignFirstResponder];
}


#pragma mark-确认充值
-(void)MCRechargeFooter_Recharge{
    
    
}

#pragma mark-计算已经拿到的textfield在屏幕中的位置
- (CGFloat)screenViewYValue:(UIView *)textfield {
    CGFloat y = 0;
    for (UIView *view = textfield; view; view = view.superview) {
        y += view.frame.origin.y;
        if ([view isKindOfClass:[UIScrollView class]]) {
            // 如果父视图是UIScrollView则要去掉内容滚动的距离
            UIScrollView* scrollView = (UIScrollView*)view;
            y -= scrollView.contentOffset.y;
        }
    }
    return y;
}


#pragma mark-键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
   
    
    // textfield的位置
    CGFloat viewY = [self screenViewYValue:self.textField];
    // 键盘的Y值
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat keyboardEndY = value.CGRectValue.origin.y;
    // 动画
    NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:duration.doubleValue animations:^{
        if (viewY+30 > keyboardEndY) {
            CGRect rect = self.tableView.frame;
            rect.origin.y += keyboardEndY - (viewY+30)-20;
            self.tableView.frame = rect;
        }
    }];
}

#pragma mark-键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notify {
    // 键盘动画时间
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //视图下沉恢复原状
    [UIView animateWithDuration:duration animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
    
}

#pragma mark-dealloc
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}

/*
 * 选择付款方式
 */
#pragma mark-MCRechargePaymentTypeDelegate
-(void)MCRechargePaymentType_ChooseType:(id)type{
    
    NSLog(@"------选择付款方式---------%@",type);
    
}

@end
















































